package com.ruoyi.web.controller.app;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysMaterialPrice;
import com.ruoyi.system.service.ISysMaterialPriceService;
import com.ruoyi.system.service.ISysMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.common.utils.PageUtils.startPage;

/**
 * 菜单信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/app")
public class AppController {

    @Autowired
    private ISysMaterialPriceService sysMaterialPriceService;

    private String prefix = "app";

    @Autowired
    private ISysMenuService menuService;

    @GetMapping("admin")
    public String admin()
    {
        return prefix + "/admin";
    }

    @GetMapping("gold_price")
    public String goldPrice()
    {
        return prefix + "/gold_price";
    }

    @GetMapping("gold_pricelive")
    public String goldPricelive()
    {
        return prefix + "/gold_pricelive";
    }

    @GetMapping("setting")
    public String setting()
    {
        return prefix + "/setting";
    }

    /**
     * 查询材料信息加价列表
     */
    @PostMapping("/list")
    @ResponseBody
    public AjaxResult list(SysMaterialPrice sysMaterialPrice)
    {
        startPage();
        List<SysMaterialPrice> list = sysMaterialPriceService.selectSysMaterialPriceList(sysMaterialPrice);
        return AjaxResult.success(list);
    }

    @PostMapping("/getOne")
    @ResponseBody
    public AjaxResult getOne(@RequestBody SysMaterialPrice sysMaterialPrice)
    {
        SysMaterialPrice one = sysMaterialPriceService.getOne(Wrappers.<SysMaterialPrice>lambdaQuery()
                .eq(SysMaterialPrice::getUserId, sysMaterialPrice.getUserId())
        );
        return AjaxResult.success(one);
    }

    /**
     * 导出材料信息加价列表
     */
    @Log(title = "材料信息加价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(@RequestBody SysMaterialPrice sysMaterialPrice)
    {
        List<SysMaterialPrice> list = sysMaterialPriceService.selectSysMaterialPriceList(sysMaterialPrice);
        ExcelUtil<SysMaterialPrice> util = new ExcelUtil<SysMaterialPrice>(SysMaterialPrice.class);
        return util.exportExcel(list, "材料信息加价数据");
    }

    /**
     * 新增材料信息加价
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存材料信息加价
     */
    @Log(title = "材料信息加价", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody SysMaterialPrice sysMaterialPrice)
    {
        SysMaterialPrice one = sysMaterialPriceService.getOne(Wrappers.<SysMaterialPrice>lambdaQuery()
                .eq(SysMaterialPrice::getUserId, sysMaterialPrice.getUserId())
        );
        if(one != null){
            sysMaterialPrice.setId(one.getId());
            sysMaterialPriceService.updateById(sysMaterialPrice);
        }else{
            sysMaterialPriceService.save(sysMaterialPrice);
        }

        return AjaxResult.success("操作成功");
    }

    /**
     * 修改材料信息加价
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysMaterialPrice sysMaterialPrice = sysMaterialPriceService.selectSysMaterialPriceById(id);
        mmap.put("sysMaterialPrice", sysMaterialPrice);
        return prefix + "/edit";
    }

    /**
     * 修改保存材料信息加价
     */
    @Log(title = "材料信息加价", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMaterialPrice sysMaterialPrice)
    {
        return AjaxResult.success(sysMaterialPriceService.updateSysMaterialPrice(sysMaterialPrice));
    }

    /**
     * 删除材料信息加价
     */
    @Log(title = "材料信息加价", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return AjaxResult.success(sysMaterialPriceService.deleteSysMaterialPriceByIds(ids));
    }

}
